package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydd.dto.AdvimgDTO;
import com.ydd.dto.AdvposDTO;
import com.ydd.entity.Advimg;
import com.ydd.entity.Advpos;
import com.ydd.mapper.AdvimgMapper;
import com.ydd.mapper.AdvposMapper;
import com.ydd.service.IAdvimgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.AdvimgVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Slf4j
@Service
public class AdvimgServiceImpl extends ServiceImpl<AdvimgMapper, Advimg> implements IAdvimgService {

    @Autowired
    private  AdvimgMapper advimgMapper;
    @Autowired
    private AdvposMapper advposMapper;
    @Override
    public Advimg rowInfo(Integer id) {

        Advimg advimg = advimgMapper.selectById(id);

        return advimg;
    }

    @Override
    public void upd(Advimg advimg) {
        advimgMapper.updateById(advimg);
    }
    @Override
    public void del(Integer id) {
        advimgMapper.deleteById(id);
    }

    @Override
    public void add(Advimg advimg) {
        advimgMapper.insert(advimg);
    }

    @Override
    public Map<String,Object> selectPage(AdvimgDTO advimgDTO) {

        Page<Advimg> page = new Page<>(advimgDTO.getPage(),advimgDTO.getLimit());
        LambdaQueryWrapper<Advimg> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.like(advimgDTO.getAdvimgdesc()!=null,Advimg::getAdvimgdesc,advimgDTO.getAdvimgdesc());
        queryWrapper.like(advimgDTO.getAdvimgpos()!=null,Advimg::getAdvimgpos,advimgDTO.getAdvimgpos());
        advimgMapper.selectPage(page,queryWrapper);

        //shaolee

        List<AdvimgVO> advimgVOList = new ArrayList<>();
        List<Advimg> records = page.getRecords();
        for (Advimg record : records) {
            AdvimgVO advimgVO = new AdvimgVO();
            //LambdaQueryWrapper<Advimg> Wrapper =
            //        new LambdaQueryWrapper<>();
            String advimgpos = record.getAdvimgpos();
            //Wrapper.like(Advimg::getAdvimgpos,advimgpos);


            int advimgposId = Integer.parseInt(advimgpos);
            Advpos adv = advposMapper.selectId(advimgposId);
            //log.info("adv:{}",adv);
            BeanUtils.copyProperties(adv,advimgVO);
            BeanUtils.copyProperties(record,advimgVO);
            advimgVOList.add(advimgVO);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getTotal());
        map.put("currentPage",page.getCurrent());
        map.put("pageSize",advimgDTO.getLimit());
        map.put("totalPage",page.getPages());
        map.put("data",advimgVOList);

        return map;
    }
}
